package com.probox.modules.controller.order;

import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.common.entity.domain.order.OrderPostSale;
import com.probox.common.entity.service.order.IOrderPostSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 订单售后Controller
 *
 * @author hds
 * @date 2021-06-07
 */
@RestController
@RequestMapping("/order/sale")
public class OrderPostSaleController extends BaseController {
    @Autowired
    private IOrderPostSaleService orderPostSaleService;

    /**
     * 查询订单售后列表
     */
    @PreAuthorize(hasPermi = "order:sale:list")
    @GetMapping("/list")
    public TableDataInfo list(OrderPostSale orderPostSale) {
        startPage();
        List<OrderPostSale> list = orderPostSaleService.queryList(orderPostSale);
        return getDataTable(list);
    }

    /**
     * 导出订单售后列表
     */
    @PreAuthorize(hasPermi = "order:sale:export")
    @Log(title = "订单售后", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderPostSale orderPostSale) throws IOException {
        List<OrderPostSale> list = orderPostSaleService.queryList(orderPostSale);
        ExcelUtil<OrderPostSale> util = new ExcelUtil<OrderPostSale>(OrderPostSale.class);
        util.exportExcel(response, list, "订单售后数据");
    }

    /**
     * 获取订单售后详细信息
     */
    @PreAuthorize(hasPermi = "order:sale:query")
    @GetMapping(value = "/{serviceUid}")
    public AjaxResult getInfo(@PathVariable("serviceUid") Long serviceUid) {
        return AjaxResult.success(orderPostSaleService.selectOrderServiceById(serviceUid));
    }

    /**
     * 新增订单售后
     */
    @PreAuthorize(hasPermi = "order:sale:add")
    @Log(title = "订单售后", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderPostSale orderPostSale) {
        return toAjax(orderPostSaleService.insertOrderService(orderPostSale));
    }

    /**
     * 售后(售后受理 或者 退货退款，换货 收货完结)
     */
    @PreAuthorize(hasPermi = "order:sale:edit")
    @Log(title = "订单售后", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderPostSale orderPostSale) {
        return toAjax(orderPostSaleService.updateOrderService(orderPostSale));
    }

    /**
     * 删除订单售后
     */
    @PreAuthorize(hasPermi = "order:sale:remove")
    @Log(title = "订单售后", businessType = BusinessType.DELETE)
    @DeleteMapping("/{serviceUids}")
    public AjaxResult remove(@PathVariable Long[] serviceUids) {
        return toAjax(orderPostSaleService.deleteOrderServiceByIds(serviceUids));
    }

    /**
     * 售后审核(退款完结，退货退款，换货 等待商家收货完结)
     * @param sale
     * @return
     */
    @PreAuthorize(hasPermi = "order:sale:edit")
    @Log(title = "订单售后", businessType = BusinessType.UPDATE)
    @PostMapping("/auditSale")
    public AjaxResult auditSale(@RequestBody OrderPostSale sale){
        return AjaxResult.success(orderPostSaleService.auditSale(sale));
    }

}
